package com.zxp.zxpsmarthomeapk.base;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * author: Created by zhangxianpeng on 2018/5/5.
 * function:封装Fragment跳转、显示等
 */

public class FragmentFactory {
    private FragmentActivity mContext;
    private static FragmentFactory factory = new FragmentFactory();
    //用于存储已创建的Fragment对象
    private Map<String, Fragment> mFragmentMap = new HashMap<>();
    private int mLayoutId;

    private FragmentFactory() {
    }

    public static FragmentFactory getInstance() {
        return factory;
    }

    //layoutId 传入布局文件的id
    public FragmentFactory init(FragmentActivity context, int layoutId) {
        this.mContext = context;
        this.mLayoutId = layoutId;
        return factory;
    }

    public Activity getParentActivity() {
        return mContext;
    }


    private <T extends Fragment> Fragment createFragment(Class<T> clazz) {
        Fragment fragment = null;
        try {
            fragment = getFragment(clazz.getName());
            FragmentTransaction fragmentTransaction = mContext.getSupportFragmentManager().beginTransaction();
            hideFragment(fragmentTransaction);
            if (fragment == null) {

                fragment = (Fragment) clazz.newInstance();
                setFragment(fragment);
                fragmentTransaction.add(mLayoutId, fragment);
                fragmentTransaction.commit();
            } else {
                fragmentTransaction.show(fragment);
                fragmentTransaction.commit();
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return fragment;
    }

    private <T extends Fragment> Fragment getFragment(String className) {
        Fragment fragment = mFragmentMap.get(className);
        return fragment;
    }

    private <T extends Fragment> void setFragment(Fragment fragment) throws InstantiationException, IllegalAccessException {
        String className = fragment.getClass().getName();
        mFragmentMap.put(className, fragment);
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        Set<String> keySet = mFragmentMap.keySet();
        for (String key : keySet) {
            Fragment fragment = mFragmentMap.get(key);
            fragmentTransaction.hide(fragment);
        }

    }

    public <T extends Fragment> T showFragment(Class<T> clazz) {
        return (T) createFragment(clazz);
    }
}
